#include "osgtooltip.h"
#include <osg/Geode>
//#include <osg/ShapeDrawable>
#include <osg/ref_ptr>
#include "resourceloader.h"
#include <osg/Texture2D>
#include <osg/stateSet>
//#include <osg/Geometry>
//#include <osg/CullFace>
//#include <osg/Viewport>
//#include <osg/BlendFunc>
//#include <osg/AlphaFunc>
//#include <osgDB/readfile>
//#include <osgText/Text>
//#include <osgText/Font>
#include <QStringList.h>
#include "utility.h"
//#include <dtDAL/enginepropertytypes.h>
//#include <osg/Material>

namespace Gui2D
{
	OsgToolTip* OsgToolTip::_osgToolTip = NULL;

	OsgToolTip& OsgToolTip::GetInstance()
	{
		if(_osgToolTip == NULL)
			_osgToolTip = new OsgToolTip();
		return *_osgToolTip;
	}

	OsgToolTip::OsgToolTip()
	{
		_backgroundOffset = osg::Vec3(0.0f,20.0f,0.0f);
		_borderUp=_borderDown=_borderLeft=_borderRight=10.0f;
		_textString = "";
		getOrCreateHudNode();
	}

	OsgToolTip::~OsgToolTip()
	{}

	osg::Node* OsgToolTip::getOrCreateHudNode()
	{
		if(!_hudNode.valid())
		{
			_hudNode = new osg::Group;
			//background
			osg::ref_ptr<osg::Geode> backgroudgeode = new osg::Geode();
			_backgroundGeo = osg::createTexturedQuadGeometry
				(osg::Vec3(0.0f,0.0f,0.0f), osg::Vec3(0.0f,0.0f,0.0f), osg::Vec3(0.0f,0.0f,-0.5f));
			backgroudgeode->addDrawable( _backgroundGeo );
			osg::ref_ptr<osg::StateSet> geometrystate2 = _backgroundGeo->getOrCreateStateSet();
			geometrystate2->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
			geometrystate2->setMode(GL_BLEND,osg::StateAttribute::ON);
			//geometrystate2->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);

			_hudNode->addChild(backgroudgeode.get());
			setBackgroundColor(osg::Vec4(1.0f,1.0f,1.0f,0.5f));
			//text
			osg::ref_ptr<osg::Geode> textgeode = new osg::Geode();
			_text = new OsgTextExp;
			if(!_font.valid())
			{
				_font = new osgText::Font();
				_font = osgText::readFontFile("fonts/simhei.ttf");
			}
			_text->setFont(_font.get());
			_text->setCharacterSize(17);
			_text->setBackdropType(osgText::Text::NONE);
			_text->setColor(osg::Vec4(0.0f,0.0f,0.0f,1.0f));
			_text->setAlignment(osgText::Text::LEFT_CENTER);
			_text->setAxisAlignment(osgText::Text::SCREEN);
			osg::StateSet* ss = _text->getOrCreateStateSet();
			textgeode->addDrawable(_text.get());
			_hudNode->addChild(textgeode.get());
		}
		return _hudNode.get();
	}

	void OsgToolTip::resizeViewSize(double width,double height)
	{
		//if(_hudNode.valid())
		//{
		//	_hudNode->setProjectionMatrix(osg::Matrix::ortho2D(0,width,0,height));
		//	_hudNode->setViewport(0,0,width,height);
		//}
		_viewWidth = width;
		_viewHeight = height;
	}

	QString OsgToolTip::getToolTipText()
	{
		return _textString;
	}

	void OsgToolTip::setToolTipText(const QString& text,int x,int y)
	{
		_textString = text;
		if(text == "")
		{
			_text->setText("");
			osg::ref_ptr<osg::Vec3Array> marray = new osg::Vec3Array;
			marray->push_back(osg::Vec3(0.0f,0.0f,-0.5f));
			marray->push_back(osg::Vec3(0.0f,0.0f,-0.5f));
			marray->push_back(osg::Vec3(0.0f,0.0f,-0.5f));
			marray->push_back(osg::Vec3(0.0f,0.0f,-0.5f));
			_backgroundGeo->setVertexArray(marray);
			//_backgroundGeo->setTexCoordArray
		}
		else
		{
			_font = new osgText::Font();
			_font = osgText::readFontFile("fonts/simhei.ttf");
			_text->setFont(_font.get());
			int rowcount = text.split("\n",QString::SkipEmptyParts).size();
			std::string	textstr	= ChineseTexttoStdstring(text);
			setlocale(LC_ALL,".936");
			char* charstr = (char*)textstr.c_str();
			int requiredsize = mbstowcs(NULL,charstr,0);
			wchar_t* wtext = new wchar_t[requiredsize+1];
			mbstowcs(wtext,charstr,requiredsize+1);
			_text->setText(wtext);
			delete wtext;
			osg::BoundingBox bb = _text->getTextBB();
			float textnodeheight = _text->getCharacterHeight()*rowcount;
			if(x == 0 && y == 0)
			{
				_backgroundOffset = osg::Vec3(0.0f,20.0f,0.0f);
				osg::ref_ptr<osg::Vec3Array> marray = new osg::Vec3Array;
				marray->push_back(osg::Vec3(_backgroundOffset.x(),_backgroundOffset.y(),-0.5f));
				marray->push_back(osg::Vec3(_viewWidth,_backgroundOffset.y(),-0.5f));
				marray->push_back(osg::Vec3(_viewWidth,_backgroundOffset.y()+textnodeheight+_borderUp+_borderDown,-0.5f));
				marray->push_back(osg::Vec3(_backgroundOffset.x(),_backgroundOffset.y()+textnodeheight+_borderUp+_borderDown,-0.5f));
				_backgroundGeo->setVertexArray(marray);
				_text->setPosition(osg::Vec3(_borderLeft,_backgroundOffset.y()+_borderDown + textnodeheight/2,0.0f));
			}
			else
			{
				_backgroundOffset.x() = x;
				_backgroundOffset.y() = y;
				float textnodewidth = bb.xMax();
				osg::ref_ptr<osg::Vec3Array> marray = new osg::Vec3Array;
				marray->push_back(osg::Vec3(_backgroundOffset.x(),_backgroundOffset.y(),-0.5f));
				marray->push_back(osg::Vec3(_backgroundOffset.x(),_backgroundOffset.y()-textnodeheight-_borderUp-_borderDown,-0.5f));
				marray->push_back(osg::Vec3(_backgroundOffset.x() + _borderLeft + _borderRight + textnodewidth,
						_backgroundOffset.y()-textnodeheight-_borderUp-_borderDown,-0.5f));
				marray->push_back(osg::Vec3(_backgroundOffset.x() + _borderLeft + _borderRight + textnodewidth,_backgroundOffset.y(),-0.5f));			
				_backgroundGeo->setVertexArray(marray);
				_text->setPosition(osg::Vec3(_backgroundOffset.x() + _borderLeft,_backgroundOffset.y()-textnodeheight/2-_borderUp,0.0f));
			}
		}
	}

	void OsgToolTip::setBackgroundColor(const osg::Vec4& backgroundcolor)
	{
		osg::ref_ptr<osg::Vec4Array> colorarray = new osg::Vec4Array ;
		colorarray->push_back( backgroundcolor ) ;
		_backgroundGeo->setColorArray(colorarray);
	}

	void OsgToolTip::setTextColor(const osg::Vec4& textcolor)
	{
		_text->setColor(textcolor);
	}

	void OsgToolTip::setTextSize(int textsize)
	{
		_text->setCharacterSize(textsize);
	}

	void OsgToolTip::setBackgroundImage(osg::ref_ptr<osg::Image> image)
	{}

	void OsgToolTip::setBackgroundImage(const QString& image)
	{
		QString str2 ="\\SceneTextures\\textures\\background\\background2.png";
		osg::ref_ptr<osg::Texture2D> geotexture = CoreBase::ResourceLoader::GetInstance().getTextureByFileName(str2).get();
		//osg::ref_ptr<osg::Texture2D> geotexture = htVRCore::EditorData::GetInstance().GetTexture2D(str2).get();
		setBackgroundTexture(geotexture);
	}

	void OsgToolTip::setBackgroundTexture(osg::ref_ptr<osg::Texture2D> btexture)
	{
		if(btexture.valid())
		{
			osg::ref_ptr<osg::StateSet> geometrystate2 = _backgroundGeo->getOrCreateStateSet();
			geometrystate2->setTextureAttributeAndModes (0,btexture,osg::StateAttribute::ON);
		}
	}
}